package String;

import org.junit.Test;

import java.util.*;

public class _49_GroupAnagrams {
    //solution 1:using hashmap to mantain where the list is,and use sort to get the same list
    public List<List<String>> groupAnagrams(String[] strs) {
        List<List<String>> results = new ArrayList<>();
        Map<String, Integer> map = new HashMap<>();
        int count = 0;
        for (int i = 0; i < strs.length; i++) {
            char[] temp = strs[i].toCharArray();
            Arrays.sort(temp);
            String ss = String.valueOf(temp);
            if (map.containsKey(ss)) {
                results.get(map.get(ss)).add(strs[i]);
            } else {
                List<String> list = new ArrayList<>();
                list.add(strs[i]);
                results.add(list);
                map.put(ss, count++);
            }
        }
        return results;
    }

    //find how the list is sorted
    //result:list is sorted by it's element's inserted order
    @Test
    public void test() {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(5);
        System.out.println(list.get(0));
        System.out.println(list.get(1));
        list.add(6);
        System.out.println(list.get(0));
        System.out.println(list.get(4));
    }
}
